﻿<?php
	if($_POST['DO_INCLUSAO']){
		$model = new core\Model('ASSOCIATE','app\libs\Association');
		$a = new app\libs\Association(0,$_REQUEST['CLASSE'],$_REQUEST['TABELA']);

		$model->beginTransaction();
		$ret = $model->create($a);
		if($ret){			
			$model->commit();
			$a = $model->read(array("ASSOCIATE"=>$ret));			
			$a = $a[0];
			if($_REQUEST['EDICAO']){
				$model = new core\Model('ASSOCIATE_ATT','app\libs\AssociationAtt');	
				$model->beginTransaction();

				foreach($_REQUEST['EDICAO'] as $e){
					if($e['COLUNA'] && $e['ATRIBUTO'] && !$e['ALTERACAO']){
						$att = new app\libs\AssociationAtt(0,$e['COLUNA'],$e['ATRIBUTO'],$e['IS_OBJ'],$a);						
						$ret = $model->create($att);
						if(!$ret){
							$model->rollback();
							$ERRO_DB = $model->getErrorMsg();
							break;
						}															
					}					
				}
			}
		}
		else{
			$model->rollback();
			$ERRO_DB = $model->getErrorMsg();
		}

		if(!$ERRO_DB){
			$model->commit();
			echo "<script>document.location.href = '/associacao/visualizacao/success';</script>";
			exit;
		}
	}

	echo "<p class='hintFail'>{$ERRO_DB}</p>";
?>
	<div class='body' style='padding-left: 15px; width: 90%; margin: auto;'><br/>
		<form method='post' action='/associacao/inclusao'>			
			<fieldset>
				<legend><b>Incluir Associação</b></legend>
				<table class='body' style='width: 100%;'>
					<tr>
						<td><b>Tabela:</b></td>
						<td><span class='k-textbox' style='width: 220px;'><input type='text' name='TABELA' size='60' maxlength='100'></span></td>
						<td><b>Classe:</b></td>
						<td><span class='k-textbox' style='width: 220px;'><input type='text' name='CLASSE' size='60' maxlength='100'></span></td>
						<td style='width: 400px;'>&nbsp;</td>
					</tr>
					<tr><td colspan='20' width: 100%><hr/></td></tr>
				</table>
				<table>
					<tr>
						<td><b>Coluna</b></td>
						<td><b>Atributo</b></td>
						<td><b>É Objeto</b></td>
					</tr>
<?php
		if($_REQUEST['EDICAO']){
			foreach ($_REQUEST[EDICAO] as $i => $e) {
	        	echo "<input type='hidden' name='EDICAO[$i][ID]' value='{$e['ID']}' />";
	        	echo "<tr>";
	        	echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][COLUNA]' /></span></td>";
	        	echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][ATRIBUTO]' /></span></td>";
	        	echo "<td style='text-align: center'><input type='checkbox' size='40' maxlength='40' name='EDICAO[{$i}][IS_OBJ]' value='S' /></td>";       	
	        	echo "</tr>";
	        } 
	        $i++;      
		}	
		if(!$ERRO_DB){
			for($seq = $i+3; $seq > $i; $i++){			
	        	echo "<tr>";
	        	echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][COLUNA]' /></span></td>";
	        	echo "<td><span class='k-textbox' style='width: 220px;'><input type='text' size='40' maxlength='40' name='EDICAO[{$i}][ATRIBUTO]' /></span></td>";
	        	echo "<td style='text-align: center'><input type='checkbox' size='40' maxlength='40' name='EDICAO[{$i}][IS_OBJ]' value='S' /></td>";	        	
	        	echo "</tr>";
			}
		}	
?>			
				</table>
				<p><input type='submit' class='k-button' value='Mais' /></p>
				<p><input type='submit' class='k-button' name="DO_INCLUSAO" value='Gravar'></p>
			</fieldset>
		</form>
	</div>